package com.graph.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ParseJson {

    /* 远程调用得到一个这样的json
        [{
		"a": {
			"name": "node1"
		},
		"labels(a)": ["Im N1"],
		"r": [{
				"name ": "node1"
			},
			"1->2",
			{
				"name": "node2"
			}
		],
		"b": {
			"name": "node2"
		},
		"labels(b)": ["Im N2"]
	},
	{
		"a": {
			"name": "node2 "
		},
		"labels(a)": ["node2"],
		"r": [{
				"name ": "node2"
			},
			"2->3",
			{
				"name": "node3"
			}
		],
		"b": {
			"name": "node3"
		},
		"labels(b)": ["Im N3"]
	}
]
     */  //表述了1-2-3这样的一个关系，现在要把这个关系解析出来
    public static void main(String[] args) {
        System.out.println("hello");
        //试着解析上面的的json字符串
        String json = "[{\n" +
                "\t\t\"a\": {\n" +
                "\t\t\t\"name\": \"node1\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(a)\": [\"Im N1\"],\n" +
                "\t\t\"r\": [{\n" +
                "\t\t\t\t\"name\": \"node1\"\n" +
                "\t\t\t},\n" +
                "\t\t\t\"1->2\",\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"name\": \"node2\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"b\": {\n" +
                "\t\t\t\"name\": \"node2\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(b)\": [\"Im N2\",\"Im dddd\"]\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"a\": {\n" +
                "\t\t\t\"name\": \"node2\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(a)\": [\"Im N2\" ,\"Im dddd\" ],\n" +
                "\t\t\"r\": [{\n" +
                "\t\t\t\t\"name\": \"node2\"\n" +
                "\t\t\t},\n" +
                "\t\t\t\"2->3\",\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"name\": \"node3\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"b\": {\n" +
                "\t\t\t\"name\": \"node3\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(b)\": [\"Im N3\"]\n" +
                "\t}\n" +
                "]";

        //使用fastjson解析这个字符串
        JSONArray jsonArray = JSON.parseArray(json);
        List<NodeRelation.Node> nodeList = new ArrayList<>();
        List<Relation> relationList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++){
            String sjson = jsonArray.get(i).toString();
            String jsonStr = sjson.replace("labels(a)", "labels_a").replace("labels(b)", "labels_b");
            System.out.println(jsonStr);

            NodeRelation nodeRelation = JSON.parseObject(jsonStr, NodeRelation.class);
            System.out.println(nodeRelation);
            //提取出里面的所有Node，并且进行去重（去重行的时候Node里面只有一个name看看是否可以去重）
            List<NodeRelation.Node> nodes = nodeRelation.getNodes();
            nodeList.addAll(nodes);

            //处理关系 数组的第一个元素是一个对象
            Object[] r = nodeRelation.getR();
            NodeRelation.Node source = JSON.parseObject(r[0].toString(), NodeRelation.Node.class);
            String value = r[1].toString();
            NodeRelation.Node target = JSON.parseObject(r[0].toString(), NodeRelation.Node.class);
            Relation relation = new Relation();
            relation.setSource(source.getName());
            relation.setValue(value != null ? value : "");
            relation.setTarget(target.getName());
            relationList.add(relation);

        }
        System.out.println(nodeList.size());
        //使用Lambda为拿到nodeList中Node对象的name属性转为数组
        //List<String> nodeNameList = nodeList.stream().map(NodeRelation.Node::getName).collect(Collectors.toList());
        //为nodeList去重 stream流的distincet方法比较的是比较的地址值
        nodeList = nodeList.stream().distinct().collect(Collectors.toList());
        System.out.println("---------------------");
        System.out.println(nodeList.size());
        System.out.println(relationList.size());
    }
}
